---
title:  Optimizing Queries on Data Partitioned by a Key or Field Value
---

<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

You can improve query performance on data that is partitioned by key or a field value by creating a key index and then executing the query using the `FunctionService` with the key or field value used as filter.

The following is an example how to optimize a query that will be run on data partitioned by region key value. In the following example, data is partitioned by the "orderId" field.

1.  Create a key index on the orderId field. See [Creating Key Indexes](../query_index/creating_key_indexes.html#concept_09E29507AF0D42CF81D261B030D0B7C8) for more details.
2.  Execute the query using the function service with orderId provided as the filter to the function context. For example:

    ``` pre
    /**
     * Execute MyFunction for query on data partitioned by orderId key
     *
     */
    public class TestFunctionQuery {

      public static void main(String[] args) {

        Set filter =  new HashSet();
        ResultCollector rcollector = null;

        //Filter data based on orderId  = '12345'
        filter.add(12345);

        //Query to get all orders that match ID 12345 and amount > 1000
        String qStr = "SELECT * FROM /Orders WHERE orderId = '12345' AND amount > 1000";

        try {
          Function func = new MyFunction("testFunction");

          Region region = CacheFactory.getAnyInstance().getRegion("myPartitionRegion");

          //Function will be routed to one node containing the bucket
          //for ID=1 and query will execute on that bucket.
          rcollector = FunctionService
              .onRegion(region)
              .withArgs(qStr)
              .withFilter(filter)
              .execute(func);

          Object result = rcollector.getResult();

          //Results from one or multiple nodes.
          ArrayList resultList = (ArrayList)result;

          List queryResults = new ArrayList();

          if (resultList.size()!=0) {
            for (Object obj: resultList) {
              if (obj != null) {
                queryResults.addAll((ArrayList)obj);
              }
            }
          }
          printResults(queryResults);

        } catch (FunctionException ex) {
            getLogger().info(ex);
        }
      }
    }
    ```


